{% extends "base.html" %}

{% block title %}
filter2D
{% endblock %}

{% block description %}
<p>Convolves an image with the kernel.</p>
{% endblock %}

{% block signature %}
<pre>cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) &rarr; dst</pre>
{% endblock %}

{% block parameters %}
<ul>
    <li><prmtr>src</prmtr> (<ptype>np.ndarray</ptype>): Input image.</li>
    <li><prmtr>ddepth</prmtr> (<a href="https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#filter_depths"><code>cv2.CV_*</code></a>): Output image depth. Default is -1 to use <code>src1.depth()</code>, and this is used in this app.</li>
    <li><prmtr>kernel</prmtr> (<ptype>np.ndarray</ptype>): Convolution kernel (or rather a correlation kernel), a single-channel floating point matrix; if you want to apply different kernels to different channels, split the image into separate color planes using <code>split()</code> and process them individually.</li>
    <li><prmtr>dst</prmtr> (optional; <ptype>np.ndarray</ptype>): Output image of the same size and type as <code>src</code>.</li>
    <li><prmtr>anchor</prmtr> (optional; <ptype>tuple: (int, int)</ptype>): Anchor of the kernel that indicates the relative position of a filtered point within the kernel. The tuple is provided as (column, row), and the anchor should lie within the kernel. Default value (-1,-1) means that the anchor is at the kernel center.</li>
    <li><prmtr>delta</prmtr> (optional; <ptype>float</ptype>): Optional value added to the filtered pixels before storing them in <code>dst</code>.</li>
    <li><prmtr>borderType</prmtr> (optional; <a href="https://docs.opencv.org/master/d2/de8/group__core__array.html#ga209f2f4869e304c82d07739337eae7c5"><code>cv2.BORDER_*</code></a>): Pixel extrapolation method. Default is BORDER_DEFAULT. Choose from:
        <ul>
            <li>BORDER_CONSTANT</li>
            <li>BORDER_REPLICATE</li>
            <li>BORDER_REFLECT</li>
            <li>BORDER_REFLECT_101 or BORDER_REFLECT101 or BORDER_DEFAULT</li>
            <li>BORDER_ISOLATED</li>
        </ul></li>
</ul>
{% endblock %}

{% block notes %}
<ul>
    <li>The function does actually compute correlation, not the convolution: $$\text{dst}(x,y) = \sum_{\substack{0 \leq x' &lt; \text{kernel.cols} \text{ and } 0 \leq y' &lt;  \text{kernel.rows}}} \text{kernel}(x', y') * \text{src}(x+x' - \text{anchor}.x, y+y'-\text{anchor}.y)$$ That is, the kernel is not mirrored around the anchor point. 
    </li>
    <li>
        If you need a real convolution, flip the kernel using <code>flip</code> and set the new anchor to <code>(kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1)</code>.
    </li>
    <li>BORDER_WRAP and BORDER_TRANSPARENT are not supported.</li>
    <li>If you want to use BORDER_CONSTANT, you should manually add border to your image with <code>copyMakeBorder</code>, otherwise the default value is set to 0.</li>
</ul>
{% endblock %}

{% block explanation %}
<p>
    The function applies an arbitrary linear filter to an image. In-place operation is supported. When the aperture is partially outside the image, the function interpolates outlier pixel values according to the specified border mode.

    The function uses the DFT-based algorithm in case of sufficiently large kernels (around \(11 \times 11\) or larger) and the direct algorithm (that uses the engine retrieved by <code>createLinearFilter()</code>) for small kernels.
</p>
{% endblock %}

{% block references %}
<ul>
    <li><a href="https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga27c049795ce870216ddfb366086b5a04">OpenCV Documentation</a></li>
</ul>

{% endblock %}